import { NzNotificationService } from 'ng-zorro-antd/notification';
import { Injectable } from '@angular/core';
import { NzModalService } from 'ng-zorro-antd/modal';
import { Observable } from 'rxjs';
import { Message } from "./message";

/**
 * 通知服务
 * wenda
 * 2019-10-08
 */
@Injectable({
  providedIn: 'root'
})
export class NotifyService {

  constructor(
    private notification: NzNotificationService,
    private modalService: NzModalService
  ) {
  }

  /**
   * 操作成功的通知调用
   * @param content 提示内容
   * @param title 提示标题，默认为提示
   */
  public success(content: string, title: string = '提示'): Observable<any> {
    return new Observable(observer => {
      this.notification.create(
        'success',
        title,
        content
      );
      observer.next(true);
    });
  }

  /**
   * 操作成功的通知调用
   * @param msgId 消息ID
   * @param args 动态内容
   */
  public successMsg(msgId: string, ...args): Observable<any> {
    return this.success(Message.getMsg(msgId, ...args));
  }

  /**
   * 操作失败的通知
   * @param content 提示内容
   * @param title 提示标题，默认为提示
   * @param okText 按钮文本，默认为确定
   */
  public error(content: string, title: string = '提示', okText: string = '确定'): Observable<any> {
    return new Observable(observer => {
      this.modalService.error({
        nzTitle: title,
        nzContent: content,
        nzOkText: okText,
        nzOnOk: () => {
          observer.next(true);
        }
      });

    });
  }

  /**
   * 操作失败的通知
   * @param msgId 消息ID
   * @param args 动态内容
   */
  public errorMsg(msgId: string, ...args): Observable<any> {
    return this.error(Message.getMsg(msgId, ...args));
  }

  /**
   * 调用确认提示框，返回0表示点击的确定按钮，返回1表示点击的取消按钮
   * @param content 提示内容
   * @param title 提示标题，默认为提示
   * @param okText 确定按钮文本，默认为确定
   * @param cancelText 取消按钮文本，默认为取消
   */
  public confirm(content: string, title: string = '提示', okText: string = '确定', cancelText: string = '取消'): Observable<any> {
    return new Observable(observer => {
      this.modalService.confirm({
        nzTitle: title,
        nzContent: content,
        nzOkText: okText,
        nzCancelText: cancelText,
        nzOnOk: () => {
          observer.next(0);
        },
        nzOnCancel: () => {
          observer.next(1);
        }
      });
    });
  }

  /**
   * 调用确认提示框，返回0表示点击的确定按钮，返回1表示点击的取消按钮
   * @param msgId 消息ID
   * @param args 动态内容
   */
  public confirmMsg(msgId: string, ...args): Observable<any> {
    return this.confirm(Message.getMsg(msgId, ...args));
  }

  /**
   * 用于警告用户的通知
   * @param content 提示内容
   * @param title 提示标题，默认为提示
   * @param okText 按钮文本，默认为确定
   */
  public warning(content: string, title: string = '提示', okText: string = '确定'): Observable<any> {
    return new Observable(observer => {
      this.modalService.warning({
        nzTitle: title,
        nzContent: content,
        nzOkText: okText,
        nzOnOk: () => {
          observer.next(true);
        }
      });
    });
  }

  /**
   * 用于警告用户的通知
   * @param msgId 消息ID
   * @param args 动态内容
   */
  public warningMsg(msgId: string, ...args): Observable<any> {
    return this.warning(Message.getMsg(msgId, ...args));
  }
}
